<?php
defined('BASEPATH') OR exit('No direct script access allowed');

class Weixin extends CI_Controller {
    public function __construct()
    {
        parent::__construct();
        $this->lw_log->systemLog($this->uri->segments,$this->input->post());
        $this->load->model('film_model');
    }

    public function dy()
    {
        //我们会post 参数到本接口 Content=用户消息&fromUsername=用户加密id&toUsername=你的公众号token
        $postStr = file_get_contents("php://input");
        $this->load->library('lw_db',array('tb_name'=>'tb_film_list'),'tb_film_list');
        $this->load->library('lw_db',array('tb_name'=>'tb_film_download'),'tb_film_download');
        $time = time();
        if (!empty($postStr)) {
            $postObj = simplexml_load_string($postStr, 'SimpleXMLElement', LIBXML_NOCDATA);
            $from = $postObj->FromUserName;
            $to = $postObj->ToUserName;
            $keyword = $postObj->Content;
            $info = $this->film_model->get_film_download($keyword);
            if($info){
                $response = "<xml>
                    <ToUserName><![CDATA[{$from}]]></ToUserName>
                    <FromUserName><![CDATA[{$to}]]></FromUserName>
                    <CreateTime>{$time}</CreateTime>
                    <MsgType><![CDATA[text]]></MsgType>
                    <Content><![CDATA[电影名称:《{$info['name']}》 \r\n迅雷地址：\r\n{$info['code']}]]></Content>
                    </xml>";
            }else{
                $response = "<xml>
                    <ToUserName><![CDATA[{$from}]]></ToUserName>
                    <FromUserName><![CDATA[{$to}]]></FromUserName>
                    <CreateTime>{$time}</CreateTime>
                    <MsgType><![CDATA[text]]></MsgType>
                    <Content><![CDATA[亲，你说的电影小编没找到，请试试其他关键字]]></Content>
                    </xml>";
            }

            echo $response;
        }
    }

    public function test()
    {

    }

}
